<?php 
/**
 * Description of form
 * @author diotheos
 * @since 7 мая 2010
 */
class App_Form_Forum_ThreadForm extends Zend_Form{
    const RECAPTCHA_PUBKEY = '6Lc9Xb8SAAAAAA67HbkRekyRPciYZLdphnJQPA6Y';
    const RECAPTCHA_PRIVKEY = '6Lc9Xb8SAAAAAD441ENv9ajj_uodIwV0tAAk9uv8';
    const BELONGS_TO = 'thread';
    
    public static $DESC_MAX_LENGTH = 100;
    public static $DESC_MIN_LENGTH = 0;
    public static $DESC_NUM_COLS = 50;
    public static $DESC_NUM_ROWS = 8;


    public static $CAPT_MAX_LENGTH = 50;
    public static $CAPT_MIN_LENGTH = 10;
    public static $CAPT_NUM_COLS = 50;
    
    public static $ORDER_MAX_LENGTH = 5;
    public static $ORDER_NUM_COLS = 5;


    public function  init() {
        parent::init();
        /**
         * Settin default parameters
         */
        $this->setMethod('post');
        /**
         * Creating Validators
         */
        
        $integerValidator = new Zend_Validate_Int();
        $integerValidator->setMessage(_('Можно использовать только целые числа'));
        
        $notEmptyValidator = new Zend_Validate_NotEmpty();
        $notEmptyValidator->setMessage(_('Поле не может быть пустым'));
        
        // Description validators
        $descriptionLengthValidator = new Zend_Validate_StringLength();
        $descriptionLengthValidator->setMax(self::$DESC_MAX_LENGTH);
        $descriptionLengthValidator->setMin(self::$DESC_MIN_LENGTH);
        $descriptionLengthValidator->setMessage(_('Введите не больше').' '.$descriptionLengthValidator->getMax().' '._('символов'), Zend_Validate_StringLength::TOO_LONG);
        $descriptionLengthValidator->setMessage(_('Введите не меньше').' '.$descriptionLengthValidator->getMin().' '._('символов'), Zend_Validate_StringLength::TOO_SHORT);
        
        // Caption validators
        $captionLengthValidator = new Zend_Validate_StringLength();
        $captionLengthValidator->setMax(self::$CAPT_MAX_LENGTH);
        $captionLengthValidator->setMin(self::$CAPT_MIN_LENGTH);
        $captionLengthValidator->setMessage(_('Введите не больше').' '.$captionLengthValidator->getMax().' '._('символов'), Zend_Validate_StringLength::TOO_LONG);
        $captionLengthValidator->setMessage(_('Введите не меньше').' '.$captionLengthValidator->getMin().' '._('символов'), Zend_Validate_StringLength::TOO_SHORT);
        
        // Ordering validators
        $orderLengthValidator = new Zend_Validate_StringLength();
        $orderLengthValidator->setMax(self::$ORDER_MAX_LENGTH);
        $orderLengthValidator->setMessage(_('Введите не больше').' '.$orderLengthValidator->getMax().' '._('символов'), Zend_Validate_StringLength::TOO_LONG);
        
        /**
         * Creating caption field
         */
        $itemCaption = new Zend_Form_Element_Text('data');
        $itemCaption->setRequired(true);
        $itemCaption->setLabel(_('Заголовок ветки'));
        $itemCaption->setBelongsTo(self::BELONGS_TO);
        $itemCaption->addValidator($notEmptyValidator, true);
        $itemCaption->addValidator($captionLengthValidator);
        $itemCaption->setAttrib('size', self::$CAPT_NUM_COLS);
        $itemCaption->setDescription(_('Введите от').' '.self::$CAPT_MIN_LENGTH.' '._('до').' ' .self::$CAPT_MAX_LENGTH. ' '._('символов'));
        $this->addElement($itemCaption);
        
        /**
         * Creating descripiton field
         */
        $itemDescription = new Zend_Form_Element_Textarea('description');
        $itemDescription->setRequired(false);
        $itemDescription->setLabel(_('Описание ветки'));
        $itemDescription->setBelongsTo(self::BELONGS_TO);
        $itemDescription->addValidator($descriptionLengthValidator);
        $itemDescription->setDescription(_('Введите от').' '.self::$DESC_MIN_LENGTH.' '._('до').' ' .self::$DESC_MAX_LENGTH. ' '._('символов'));
        $itemDescription->setAttrib('cols', self::$DESC_NUM_COLS);
        $itemDescription->setAttrib('rows', self::$DESC_NUM_ROWS);
        $this->addElement($itemDescription);
        
        /**
         * Creating ordering field
         */
        $itemOrdering = new Zend_Form_Element_Text('ordering');
        $itemOrdering->setValue(0);
        $itemOrdering->setLabel(_('Сортировка'));
        $itemOrdering->setRequired(true);
        $itemOrdering->setBelongsTo(self::BELONGS_TO);
        $itemOrdering->addValidator($integerValidator);
        $itemOrdering->setDescription('Только целое число');
        $this->addElement($itemOrdering);
        
//        /**
//         * Creating captcha field
//         */
//        $recaptcha = new Zend_Service_ReCaptcha(self::RECAPTCHA_PUBKEY, self::RECAPTCHA_PRIVKEY);
//        $threadCaptcha = new Zend_Form_Element_Captcha('captcha',array(
//            'captcha'        => 'ReCaptcha',
//            'captchaOptions' => array('captcha' => 'ReCaptcha', 'service' => $recaptcha),
//            'ignore' => true
//            )
//        );
//        $this->addElement($threadCaptcha);
        
        /**
         * Creatin publish element
         */
        $itemEnabled = new Zend_Form_Element_Checkbox('enabled');
        $itemEnabled->setLabel(_('Опубликовать'));
        $itemEnabled->setChecked(true);
        $itemEnabled->setCheckedValue(true);
        $itemEnabled->setUncheckedValue(false);
        $itemEnabled->setBelongsTo(self::BELONGS_TO);
        $this->addElement($itemEnabled);
        
        /**
         * Creating submit
         */
        $itemSubmit = new Zend_Form_Element_Submit('submit');
        $itemSubmit->setValue(_('Сохранить'));
        $itemSubmit->setLabel($itemSubmit->getValue());
        $itemSubmit->setBelongsTo(self::BELONGS_TO);
        $this->addElement($itemSubmit);
        
        return $this;
    }
}